package pedongi.mobile.colorfinder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import pedongi.mobile.colorfinder.helper.InferenceEngine;
import pedongi.mobile.colorfinder.helper.Interviewer;
import pedongi.mobile.colorfinder.knowledgebase.ColorSet;
import pedongi.mobile.colorfinder.knowledgebase.QuestionSet;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class FindActivity extends Activity {
	private Button finishButton;
	private Button viewExplanationButton , nextButton;
	private TextView questionTextView , explanationTextView;
	private RadioGroup questionOptionGroup;
	private ArrayList<RadioButton> optionRadioButtons;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find);
        
        ///Initializing private attr
        finishButton = (Button) findViewById(R.id.finishButton);
        viewExplanationButton = (Button) findViewById(R.id.viewExplanationButton);
        nextButton = (Button) findViewById(R.id.nextButton);
        questionTextView = (TextView) findViewById(R.id.questionTextView);
        explanationTextView = (TextView) findViewById(R.id.explanation);
        questionOptionGroup = (RadioGroup) findViewById(R.id.questionOptionGroup);
        optionRadioButtons = new ArrayList<RadioButton>();
        
		this.setTitle("Find Answer");
        
        setListener();
        initialize();
    }
	
	private void clearView(){
		questionOptionGroup.removeAllViews();
		optionRadioButtons.clear();
		//System.gc();
	}
	
	private void ask() {
		String question = Interviewer.getInstance().getCurrentQuestionSet().getQuestion();
		questionTextView.setText(question);
		
		ArrayList<String> options = Interviewer.getInstance().getCurrentQuestionSet().getOption();
		int i = 0;
		RadioButton rb;
		for (String option : options) {
			rb = new RadioButton(this);
			rb.setText(option);
			optionRadioButtons.add(rb);
			questionOptionGroup.addView(rb);
			i++;
		}
	}
	
	private void initialize() {
		finishButton.setVisibility(View.GONE);
		viewExplanationButton.setVisibility(View.GONE);
		
		Interviewer.getInstance().initialize();
		ask();
	}
	
	private void nextQuestion() {
		int selectedIndex = 0;
		for (int i = 0 ; i < optionRadioButtons.size() ; i++) {
			if (optionRadioButtons.get(i).isChecked())
				selectedIndex = i;
		}
		
		Interviewer.getInstance().advance(selectedIndex);
		clearView();
		
		if(Interviewer.getInstance().getCurrentQuestionIdx() >= QuestionSet.MAX_QUESTION) {
			//questionTextView.setVisibility(View.GONE);
			
			InferenceEngine.getInstance().generateAttributes(getAssets() , Interviewer.getInstance().getAnswerSet());
			String answerSet = InferenceEngine.getInstance().getSetNumber();
			ColorSet colorSet = new ColorSet(answerSet , getSharedPreferences(EditKnowledgeActivity.ANSWER_SET_PREF, 0));
			questionTextView.setText("Warna yang cocok untuk ruangan anda adalah " + colorSet.toString());
			
			//questionTextView.setText(InferenceEngine.getInstance().getAttributes()[4]);
			
			nextButton.setVisibility(View.GONE);
			
			finishButton.setVisibility(View.VISIBLE);
			viewExplanationButton.setVisibility(View.VISIBLE);
		} else
		{
			ask();
		}
	}
	
	private void setListener() {
		finishButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		viewExplanationButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				explanationTextView.setText(InferenceEngine.getInstance().getExplanation());
			}
		});
		
		nextButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				///check
				boolean checked = false;
				for (RadioButton rb : optionRadioButtons) {
					if (rb.isChecked())
						checked = true;
				}
				
				if (checked)
					nextQuestion();
			}
		});
	}
}
